export default class Dep {
  constructor() {
    this.subs = [];
  }
  //添加依赖
  addSub(sub) {
    this.subs.push(sub);
  }
  //删除依赖
  removeSub(sub) {
    remove(this.subs, sub);
  }
  //修改依赖
  depend() {
    if (window.target) {
      this.addSub(window.target);
    }
  }
  //通知
  notify() {
    const subs = this.subs.slice();
    for (let i = 0, l = subs.length; i < l; i++) {
      subs[i].update();
    }
  }
}

/**
 *
 * @param {Array} arr 依赖数组
 * @param {Any} item 依赖项
 */
function remove(arr, item) {
  if (arr.length) {
    const index = arr.indexOf(item);
    if (index > -1) {
      return arr.splice(index, 1);
    }
  }
}
